package com.fan.shop.businesslogic;

import java.io.File;
import java.math.BigDecimal;
import java.util.Date;

import org.apache.log4j.Logger;

import com.fan.shop.domain.Item;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.BrandService;
import com.fan.shop.services.CategoryService;
import com.fan.shop.services.ItemService;
import com.fan.shop.services.LocationService;
import com.fan.shop.services.TransactionReferenceService;

public class ItemCreateProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger(ItemCreateProcess.class);
	private String itemCode;
	private String itemName;
	private String itemDescription;
	private BigDecimal price;
	private int itemOrigLocation;
	private int itemCategory;
	private String itemIcon;
	private String itemAttributes;
	private int suspended;
	private Date registeredTime;
	
	private String brand;

	private String transactionReferenceNumber;

	private ItemService itemService;
	private CategoryService categoryService;
	private LocationService locationService;
	private BrandService brandService;
	
	private TransactionReferenceService transactionReferenceService;

	// private Part iconFile;
	//
	// private String iconFileContentType;

	private File iconTempFile;

	public String getItemCode() {
		return itemCode;
	}

	public void setItemCode(String itemCode) {
		this.itemCode = itemCode;
	}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public String getItemDescription() {
		return itemDescription;
	}

	public void setItemDescription(String itemDescription) {
		this.itemDescription = itemDescription;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public int getItemOrigLocation() {
		return itemOrigLocation;
	}

	public void setItemOrigLocation(int itemOrigLocation) {
		this.itemOrigLocation = itemOrigLocation;
	}

	public int getItemCategory() {
		return itemCategory;
	}

	public void setItemCategory(int itemCategory) {
		this.itemCategory = itemCategory;
	}

	public String getItemIcon() {
		return itemIcon;
	}

	public void setItemIcon(String itemIcon) {
		this.itemIcon = itemIcon;
	}

	public int isSuspended() {
		return suspended;
	}

	public void setSuspended(int suspended) {
		this.suspended = suspended;
	}

	public Date getRegisteredTime() {
		return registeredTime;
	}

	public void setRegisteredTime(Date registeredTime) {
		this.registeredTime = registeredTime;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public BrandService getBrandService() {
		return brandService;
	}

	public void setBrandService(BrandService brandService) {
		this.brandService = brandService;
	}

	public String getTransactionReferenceNumber() {
		return transactionReferenceNumber;
	}

	public void setTransactionReferenceNumber(String transactionReferenceNumber) {
		this.transactionReferenceNumber = transactionReferenceNumber;
	}

	public ItemService getItemService() {
		return itemService;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public LocationService getLocationService() {
		return locationService;
	}

	public void setLocationService(LocationService locationService) {
		this.locationService = locationService;
	}

	public TransactionReferenceService getTransactionReferenceService() {
		return transactionReferenceService;
	}

	public void setTransactionReferenceService(
			TransactionReferenceService transactionReferenceService) {
		this.transactionReferenceService = transactionReferenceService;
	}

	public String getItemAttributes() {
		return itemAttributes;
	}

	public void setItemAttributes(String itemAttributes) {
		this.itemAttributes = itemAttributes;
	}

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		ItemCreateProcess.logger = logger;
	}

	public int getSuspended() {
		return suspended;
	}

	public File getIconTempFile() {
		return iconTempFile;
	}

	public void setIconTempFile(File iconTempFile) {
		this.iconTempFile = iconTempFile;
	}

	@Override
	public Integer processImpl() {

		if (!transactionReferenceService
				.hasTransactionReference(transactionReferenceNumber)) {
			setErrorMessage("Transaction Reference Number: "
					+ transactionReferenceNumber + " doesn't exists!");
			return ShopGlobalVariables.FAILED;
		}
		if (categoryService.getCategory(itemCategory) == null) {
			setErrorMessage("Category: " + itemCategory + " doesn't exists!");
			return ShopGlobalVariables.FAILED;
		}
		if (!locationService.hasLocation(itemOrigLocation)) {
			setErrorMessage("Location: " + itemOrigLocation
					+ " doesn't exists!");
			return ShopGlobalVariables.FAILED;
		}
		if (!brandService.hasBrand(brand)) {
			setErrorMessage("Brand: " + brand
					+ " doesn't exists!");
			return ShopGlobalVariables.FAILED;
		}

		NewNumberGenerationProcess newNumberGenerationProcess = new NewNumberGenerationProcess();
		newNumberGenerationProcess
				.setTransactionReferenceService(transactionReferenceService);
		newNumberGenerationProcess
				.setTransactionCode(transactionReferenceNumber);
		int result = newNumberGenerationProcess.process();
		if (result == ShopGlobalVariables.FAILED) {
			setErrorMessage("Error when creating item no!");

			return ShopGlobalVariables.FAILED;
		}
		itemCode = newNumberGenerationProcess.getNewNumber();

		Item item = new Item();
		item.setItemCode(itemCode);
		item.setItemName(itemName);
		item.setItemDescription(itemDescription);
		item.setPrice(price);
		item.setOrigLocation(itemOrigLocation);
		item.setCategory(itemCategory);

		item.setAttributes(itemAttributes);
		item.setSuspended(suspended);
		item.setRegisteredTime(new Date(System.currentTimeMillis()));
		item.setBrand(brand);
		try {
			String iconFilePath = saveIconFile();
			if (iconFilePath == null) {
				return ShopGlobalVariables.FAILED;
			} else {
				itemIcon = iconFilePath;
			}
			item.setIcon(itemIcon);

			itemService.addItem(item);
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage(e.getMessage());
			return ShopGlobalVariables.FAILED;
		}

		return ShopGlobalVariables.SUCCESS;
	}

	private String saveIconFile() {
		FileSaveProcess fileSaveProcess = new FileSaveProcess();
		fileSaveProcess.setFile(iconTempFile);
		fileSaveProcess.setUploadType(ShopGlobalVariables.ICON);
		fileSaveProcess.setFileName(itemCode);
		int result = fileSaveProcess.process();

		if (result == ShopGlobalVariables.SUCCESS) {
			return fileSaveProcess.getRelativePath();
		} else {
			return null;
		}

	}

}
